﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SPA_Prototype.Model
{
    public class BackupManagement
    {
        private DatabaseDataSet _Dataset;
        private DatabaseDataSet.BackupTableDataTable _DataTable;
        private DatabaseDataSetTableAdapters.BackupTableTableAdapter _Adt;
        private int _interval;

        // constructor
        public BackupManagement(DatabaseDataSet dt)
        {
            _Dataset = dt;
            _DataTable = _Dataset.BackupTable;

            _Adt = new DatabaseDataSetTableAdapters.BackupTableTableAdapter();
            LoadDB();

            if (_DataTable.Rows.Count <= 0)
            {
                _DataTable.AddBackupTableRow(false, "", Application.StartupPath + "\\backup", 3, 0, DateTime.Now);
                UpdateDB();
            }
        }
        // property
        public bool Auto;
        public string EmailAddr;
        public string Folder;
        public int Period;
        public string Date;
        public int Interval
        {
            get { return _interval; }
            set
            {
                _interval = value;
                _DataTable[0].Interval = _interval;
            }
        }

        // method
        public void Update(bool auto, string email, string folder, int period)
        {
            if (_DataTable.Rows.Count > 0)
            {
                _DataTable[0].Auto = auto;
                _DataTable[0].EmailAddr = email;
                _DataTable[0].Folder = folder;
                _DataTable[0].Period = period;
                if (period <= _DataTable[0].Interval) _DataTable[0].Interval = 0;
                _DataTable[0].Date = DateTime.Now;
            }
            else _DataTable.AddBackupTableRow(auto, email, folder, period, 0, DateTime.Now);
            UpdateDB();
        }

        private void LoadDB()
        {
            _Adt.Fill(_DataTable);
            if (_DataTable.Rows.Count > 0)
            {
                Auto = _DataTable[0].Auto;
                EmailAddr = _DataTable[0].EmailAddr;
                Folder = _DataTable[0].Folder;
                Period = _DataTable[0].Period;
                Interval = _DataTable[0].Interval;
                Date = string.Format("{0:M/d/yyyy}", _DataTable[0].Date);
            }
            else
            {
                Auto = false;
                EmailAddr = "";
                Folder = Application.StartupPath + "\\backup";
                Period = 0;
                Date = string.Format("{0:M/d/yyyy}", DateTime.Now);
            }

        }

        private void UpdateDB()
        {
            try
            {
                _Adt.Update(_DataTable);
                MessageBox.Show("Successful", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
